<template>
<div>
  <el-dialog
    :title="title"
    size="small"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="车牌号" prop="carno"
                :rules="[
                  {required: true, message:'车牌号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.carno" placeholder="请填写车牌号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="品牌" prop="brand"
                :rules="[
                  {required: true, message:'品牌不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.brand" placeholder="请填写品牌"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="车型" prop="cartype"
                :rules="[
                  {required: true, message:'车型不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.cartype" placeholder="请填写车型"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="生产日期" prop="productdate"
                :rules="[
                  {required: true, message:'生产日期不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      v-model="inputForm.productdate"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="车架号" prop="carframeno"
                :rules="[
                  {required: true, message:'车架号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.carframeno" placeholder="请填写车架号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="发动机号" prop="engineno"
                :rules="[
                  {required: true, message:'发动机号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.engineno" placeholder="请填写发动机号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="车辆状态" prop="carstatus"
                :rules="[
                  {required: true, message:'车辆状态不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.carstatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('carstatus')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="行车证" prop="licence "
                :rules="[
                  {required: true, message:'行车证不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.licence " placeholder="请填写行车证"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="行车证照片" prop="licencepic"
                :rules="[
                  {required: true, message:'行车证照片不能为空', trigger:'blur'}
                 ]">
              <el-upload ref="licencepic"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl.oa.carinfo/oaCarinfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.licencepic = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.licencepic = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="licencepicArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
    <el-col :span="24">
     <el-form-item label-width="0">
            <el-tabs v-model="oaCarinfoTab">
            <el-tab-pane label="保险记录">
                  <el-button size="small" @click="addOaCarinfoInsuranceLogRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.oaCarinfoInsuranceLogList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="joindate"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="参保日期">
                  </el-table-column>
                <el-table-column
                    prop="enddate"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="到期日期">
                  </el-table-column>
                <el-table-column
                    prop="content"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="参保内容">
                  </el-table-column>
                <el-table-column
                    prop="price"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="金额">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewOaCarinfoInsuranceLogRow(scope.row)" type="text" size="mini">查看</el-button>
                      <el-button  @click="editOaCarinfoInsuranceLogRow(scope.row)" type="text" size="mini">编辑</el-button>
                      <el-button  @click="delOaCarinfoInsuranceLogRow(scope.row)" type="text" size="mini">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            <el-tab-pane label="车辆事故记录">
                  <el-button size="small" @click="addOaCarinfoAccidentLogRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.oaCarinfoAccidentLogList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="accidentdate"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="事故时间">
                  </el-table-column>
                <el-table-column
                    prop="place"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="事故地点">
                  </el-table-column>
                <el-table-column
                    prop="content"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="事故内容">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewOaCarinfoAccidentLogRow(scope.row)" type="text" size="mini">查看</el-button>
                      <el-button  @click="editOaCarinfoAccidentLogRow(scope.row)" type="text" size="mini">编辑</el-button>
                      <el-button  @click="delOaCarinfoAccidentLogRow(scope.row)" type="text" size="mini">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            <el-tab-pane label="车辆保养记录">
                  <el-button size="small" @click="addOaCarinfoMaintainLogRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.oaCarinfoMaintainLogList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="nowkilometre"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="当前公里数">
                  </el-table-column>
                <el-table-column
                    prop="content"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="保养内容">
                  </el-table-column>
                <el-table-column
                    prop="nextkilometre"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="下次保养里程">
                  </el-table-column>
                <el-table-column
                    prop="maintaindate"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="保养日期">
                  </el-table-column>
                <el-table-column
                    prop="place"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="保养地址">
                  </el-table-column>
                <el-table-column
                    prop="price"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="保养价格">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewOaCarinfoMaintainLogRow(scope.row)" type="text" size="mini">查看</el-button>
                      <el-button  @click="editOaCarinfoMaintainLogRow(scope.row)" type="text" size="mini">编辑</el-button>
                      <el-button  @click="delOaCarinfoMaintainLogRow(scope.row)" type="text" size="mini">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            <el-tab-pane label="车辆维修记录">
                  <el-button size="small" @click="addOaCarinfoRepairLogRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.oaCarinfoRepairLogList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="nowkilometre"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="当前公里数">
                  </el-table-column>
                <el-table-column
                    prop="content"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="维修内容">
                  </el-table-column>
                <el-table-column
                    prop="repairdate"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="维修日期">
                  </el-table-column>
                <el-table-column
                    prop="place"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="维修地址">
                  </el-table-column>
                <el-table-column
                    prop="price"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="维修费用">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewOaCarinfoRepairLogRow(scope.row)" type="text" size="mini">查看</el-button>
                      <el-button  @click="editOaCarinfoRepairLogRow(scope.row)" type="text" size="mini">编辑</el-button>
                      <el-button  @click="delOaCarinfoRepairLogRow(scope.row)" type="text" size="mini">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            <el-tab-pane label="车辆年检记录">
                  <el-button size="small" @click="addOaCarinfoYearcheckLogRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.oaCarinfoYearcheckLogList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="checkdate"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="年检日期">
                  </el-table-column>
                <el-table-column
                    prop="cotent"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="年检内容">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewOaCarinfoYearcheckLogRow(scope.row)" type="text" size="mini">查看</el-button>
                      <el-button  @click="editOaCarinfoYearcheckLogRow(scope.row)" type="text" size="mini">编辑</el-button>
                      <el-button  @click="delOaCarinfoYearcheckLogRow(scope.row)" type="text" size="mini">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
           </el-form-item>
          </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
  <OaCarinfoInsuranceLogForm ref="oaCarinfoInsuranceLogForm" @addRow="saveOaCarinfoInsuranceLogRow(arguments)"></OaCarinfoInsuranceLogForm>
  <OaCarinfoAccidentLogForm ref="oaCarinfoAccidentLogForm" @addRow="saveOaCarinfoAccidentLogRow(arguments)"></OaCarinfoAccidentLogForm>
  <OaCarinfoMaintainLogForm ref="oaCarinfoMaintainLogForm" @addRow="saveOaCarinfoMaintainLogRow(arguments)"></OaCarinfoMaintainLogForm>
  <OaCarinfoRepairLogForm ref="oaCarinfoRepairLogForm" @addRow="saveOaCarinfoRepairLogRow(arguments)"></OaCarinfoRepairLogForm>
  <OaCarinfoYearcheckLogForm ref="oaCarinfoYearcheckLogForm" @addRow="saveOaCarinfoYearcheckLogRow(arguments)"></OaCarinfoYearcheckLogForm>
</div>
</template>

<script>
  import OaCarinfoInsuranceLogForm from './OaCarinfoInsuranceLogForm'
  import OaCarinfoAccidentLogForm from './OaCarinfoAccidentLogForm'
  import OaCarinfoMaintainLogForm from './OaCarinfoMaintainLogForm'
  import OaCarinfoRepairLogForm from './OaCarinfoRepairLogForm'
  import OaCarinfoYearcheckLogForm from './OaCarinfoYearcheckLogForm'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        oaCarinfoTab: '0',
        licencepicArra: [],
        inputForm: {
          id: '',
          oaCarinfoInsuranceLogList: [],
          oaCarinfoAccidentLogList: [],
          oaCarinfoMaintainLogList: [],
          oaCarinfoRepairLogList: [],
          oaCarinfoYearcheckLogList: [],
          carno: '',
          brand: '',
          cartype: '',
          productdate: '',
          carframeno: '',
          engineno: '',
          carstatus: '',
          licence : '',
          licencepic: ''
        }
      }
    },
    components: {
      OaCarinfoInsuranceLogForm,
      OaCarinfoAccidentLogForm,
      OaCarinfoMaintainLogForm,
      OaCarinfoRepairLogForm,
      OaCarinfoYearcheckLogForm
    },
    methods: {
      init (method, id) {
        this.method = method
        if (method === 'add') {
          this.title = `新建车辆信息`
        } else if (method === 'edit') {
          this.title = '修改车辆信息'
        } else if (method === 'view') {
          this.title = '查看车辆信息'
        }
        this.licencepicArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.oaCarinfoTab = '0'
          this.inputForm.oaCarinfoInsuranceLogList = []
          this.inputForm.oaCarinfoAccidentLogList = []
          this.inputForm.oaCarinfoMaintainLogList = []
          this.inputForm.oaCarinfoRepairLogList = []
          this.inputForm.oaCarinfoYearcheckLogList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/oa/carinfo/oaCarinfo/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.oaCarinfo)
              this.inputForm.licencepic.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.licencepicArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      saveOaCarinfoInsuranceLogRow (child) {
        if (child[0] === '') {
          this.inputForm.oaCarinfoInsuranceLogList.push(child[1])
        } else {
          this.inputForm.oaCarinfoInsuranceLogList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.oaCarinfoInsuranceLogList.splice(index, 1, child[1])
            }
          })
        }
      },
      addOaCarinfoInsuranceLogRow (child) {
        this.$refs.oaCarinfoInsuranceLogForm.init('add')
      },
      viewOaCarinfoInsuranceLogRow (child) {
        this.$refs.oaCarinfoInsuranceLogForm.init('view', child)
      },
      editOaCarinfoInsuranceLogRow (child) {
        this.$refs.oaCarinfoInsuranceLogForm.init('edit', child)
      },
      delOaCarinfoInsuranceLogRow (child) {
        this.inputForm.oaCarinfoInsuranceLogList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.oaCarinfoInsuranceLogList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.oaCarinfoInsuranceLogList.splice(index, 1, item)
          }
        })
      },
      saveOaCarinfoAccidentLogRow (child) {
        if (child[0] === '') {
          this.inputForm.oaCarinfoAccidentLogList.push(child[1])
        } else {
          this.inputForm.oaCarinfoAccidentLogList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.oaCarinfoAccidentLogList.splice(index, 1, child[1])
            }
          })
        }
      },
      addOaCarinfoAccidentLogRow (child) {
        this.$refs.oaCarinfoAccidentLogForm.init('add')
      },
      viewOaCarinfoAccidentLogRow (child) {
        this.$refs.oaCarinfoAccidentLogForm.init('view', child)
      },
      editOaCarinfoAccidentLogRow (child) {
        this.$refs.oaCarinfoAccidentLogForm.init('edit', child)
      },
      delOaCarinfoAccidentLogRow (child) {
        this.inputForm.oaCarinfoAccidentLogList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.oaCarinfoAccidentLogList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.oaCarinfoAccidentLogList.splice(index, 1, item)
          }
        })
      },
      saveOaCarinfoMaintainLogRow (child) {
        if (child[0] === '') {
          this.inputForm.oaCarinfoMaintainLogList.push(child[1])
        } else {
          this.inputForm.oaCarinfoMaintainLogList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.oaCarinfoMaintainLogList.splice(index, 1, child[1])
            }
          })
        }
      },
      addOaCarinfoMaintainLogRow (child) {
        this.$refs.oaCarinfoMaintainLogForm.init('add')
      },
      viewOaCarinfoMaintainLogRow (child) {
        this.$refs.oaCarinfoMaintainLogForm.init('view', child)
      },
      editOaCarinfoMaintainLogRow (child) {
        this.$refs.oaCarinfoMaintainLogForm.init('edit', child)
      },
      delOaCarinfoMaintainLogRow (child) {
        this.inputForm.oaCarinfoMaintainLogList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.oaCarinfoMaintainLogList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.oaCarinfoMaintainLogList.splice(index, 1, item)
          }
        })
      },
      saveOaCarinfoRepairLogRow (child) {
        if (child[0] === '') {
          this.inputForm.oaCarinfoRepairLogList.push(child[1])
        } else {
          this.inputForm.oaCarinfoRepairLogList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.oaCarinfoRepairLogList.splice(index, 1, child[1])
            }
          })
        }
      },
      addOaCarinfoRepairLogRow (child) {
        this.$refs.oaCarinfoRepairLogForm.init('add')
      },
      viewOaCarinfoRepairLogRow (child) {
        this.$refs.oaCarinfoRepairLogForm.init('view', child)
      },
      editOaCarinfoRepairLogRow (child) {
        this.$refs.oaCarinfoRepairLogForm.init('edit', child)
      },
      delOaCarinfoRepairLogRow (child) {
        this.inputForm.oaCarinfoRepairLogList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.oaCarinfoRepairLogList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.oaCarinfoRepairLogList.splice(index, 1, item)
          }
        })
      },
      saveOaCarinfoYearcheckLogRow (child) {
        if (child[0] === '') {
          this.inputForm.oaCarinfoYearcheckLogList.push(child[1])
        } else {
          this.inputForm.oaCarinfoYearcheckLogList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.oaCarinfoYearcheckLogList.splice(index, 1, child[1])
            }
          })
        }
      },
      addOaCarinfoYearcheckLogRow (child) {
        this.$refs.oaCarinfoYearcheckLogForm.init('add')
      },
      viewOaCarinfoYearcheckLogRow (child) {
        this.$refs.oaCarinfoYearcheckLogForm.init('view', child)
      },
      editOaCarinfoYearcheckLogRow (child) {
        this.$refs.oaCarinfoYearcheckLogForm.init('edit', child)
      },
      delOaCarinfoYearcheckLogRow (child) {
        this.inputForm.oaCarinfoYearcheckLogList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.oaCarinfoYearcheckLogList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.oaCarinfoYearcheckLogList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/oa/carinfo/oaCarinfo/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>